"""
@name : zuoye
@author : qhy
@projectname: tiannongbabu
"""


# 注册、登录
class Danli:
    instance = None

    def __new__(cls, *args, **kwargs):
        if cls.instance is None:
            cls.instance = object.__new__(cls)
        return cls.instance

    def __init__(self, name, passwd, money):
        self.name = name
        self.passwd = passwd
        self.money = money

    def register(self,newname, newpasswd, newmoney):
        if newname not in User(name):
            newuser = User(newname, newpasswd, newmoney)

    def login(self,name, passwd):
        if name in User(name):
            if passwd in User(passwd):
                # 但是没判断是否是对应账户的密码？
                return True
            else:
                print("用户密码错误")
        else:
            print("用户不存在,请注册！")


class User:
    instance = None

    def __new__(cls, *args, **kwargs):
        if cls.instance is None:
            cls.instance = object.__new__(cls)
        return cls.instance

    def __init__(self, name, passwd, money):
        self.name = name
        self.passwd = passwd
        self.money = money

    def buy_goods(self, goods,weight):
        if weight > goods.store:
            print("库存不足！")
        else:
            pay = weight * goods.price
            if self.money >= pay:
                self.money -= pay
                goods.store -= weight
                print(f"购买成功，{self.name}剩余{self.money}元，{goods.product_name}剩余库存{goods.store}")
            else:
                print("余额不足！")

    def buy_information(self):
        # 保存每次购物的信息
        with open("buy_history", 'w+', encoding="ufr-8")as fp:
            fp.write(name, self.money, weight, goods.product_name, '\n')


class Goods:
    def __init__(self, classify, product_name, price, store):
        self.classify = classify
        self.product_name = product_name
        self.price = price
        self.store = store


class Interface:
    def menu(self):
        print("1.注册")
        print("2.登录")
        print("3.购买")
        print("4.查询购物信息")
        print("5.退出")


while True:
    print("欢迎来购物，祝您购物愉快".center(10, "*"))
    print("请先输入2登录后购买哦！")
    Interface.menu(self=0)
    option = input("请输入数字1~5:")

    if option == '1':
        newname = input("请输入名字:")
        newpasswd = input("请输入密码：")
        newmoney = input("请输入金额：")
        Danli.register(newname, newpasswd, newmoney)

    if option == '2':
        name = input("请输入名字:")
        passwd = input("请输入密码：")
        Danli.login(name, passwd)

    if option == '3':
        goods = input("请输入你想要的商品：")
        weight = input("请输入你想要的商品斤数:")
        User.buy_goods(goods, weight)

    if option == '4':
        name = input("请输入你想查询的人员名字：")
        with open("buy_history", 'w+', encoding="ufr-8")as fp:
            fp.readline(name)

    if option == '5':
        break

user1 = User("yan", "123456", 5000)
user2 = User("qhy", "1234567", "8000")

apple = Goods("fruits", "apple", 8, 30)
orange = Goods("fruits", "orange", 4, 100)

user1.buy_goods(apple, 3)


###链表的反转
class ListNode(object):
    '''定义创建链表节点的类'''

    def __init__(self, val, next=None):
        self.val = val
        self.next = next


# 方法一：递归实现
def resverse_linked_list(head):
    if not head:
        return head
    if not head.next:
        return head
    head_node = resverse_linked_list(head.next)
    head.next.next = head
    head.next = None
    return head_node


# 方法二：栈实现。先把所有节点入栈，然后弹出第一个便是新的头指针，最后依次弹出并连接
def resverse_linked_list_stack(head):
    stack = [None]
    while head:
        stack.append(head)
        head = head.next
    head = stack.pop()
    cur = head
    while stack:
        cur.next = stack.pop()
        cur = cur.next
    return head